public class GetRainWater {
}
class GetRainWaterSolution {
    public int trap(int[] height) {
        int n = height.length;
        int lmax = 0;
        int rmax = n-1;
        int l = 1;
        int r = n-2;
        int size = 0;
        while(l<=r){
            if(height[lmax]<height[rmax]){
                size += Math.max(0,height[lmax]-height[l]);
                if(height[lmax]<height[l]){
                    lmax = l;
                }
                l++;
            }else{
                size += Math.max(0,height[rmax]-height[r]);
                if(height[rmax]<height[r]){
                    rmax = r;
                }
                r--;
            }
        }
        return size;
    }
}